package com.uchengwang.ui.cm_ui.me.fm;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chanven.lib.cptr.PtrClassicFrameLayout;
import com.chanven.lib.cptr.PtrDefaultHandler;
import com.chanven.lib.cptr.PtrFrameLayout;
import com.chanven.lib.cptr.loadmore.OnLoadMoreListener;
import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;
import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.config.UserConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.HttpCallBack;
import com.uchengwang.functions.http.manager.HttpManager;
import com.uchengwang.ui.base_ui.basefm.BaseFragment;
import com.uchengwang.ui.cm_ui.login.bean.UserBean;
import com.uchengwang.ui.cm_ui.me.adapter.AddClubMemberAdapter;
import com.uchengwang.ui.cm_ui.ucity.cityBean.UserBeanList;
import com.uchengwang.ui.utils.JsonUtils;
import com.uchengwang.ui.utils.UCommonUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by U城网 on 2017/8/2.
 * 添加社团成员 从粉丝里
 */

public class FragmentClubMemberFans extends BaseFragment{
    private View view;

    private RecyclerView rv;
    private PtrClassicFrameLayout refresh;
    private RecyclerAdapterWithHF mAdapter;
    private AddClubMemberAdapter adapter;

    private int page = 1;//当前页  每页请求多少数据
    private int totlaPagers ;//总页数【服务器返回】


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        setLayoutId(R.layout.fragment_school_ba);
        view = super.onCreateView(inflater, container, savedInstanceState);

        initView(view);// 控件初始化
        initData();
        return view;
    }

    private void initView(View view){
        refresh = (PtrClassicFrameLayout) view.findViewById(R.id.fg_ba_refresh);
        rv = (RecyclerView) view.findViewById(R.id.fg_ba_rv);


    }
    private void initData(){
        rv.setLayoutManager(new LinearLayoutManager(this.getContext()));
        adapter = new AddClubMemberAdapter(this.getContext());
        mAdapter = new RecyclerAdapterWithHF(adapter);
        rv.setAdapter(mAdapter);


        refresh.setAutoLoadMoreEnable(true);
        refresh.setLoadMoreEnable(true);

        refresh.postDelayed(new Runnable() {

            @Override
            public void run() {
                refresh.autoRefresh(true);
            }
        }, 150);

        refresh.setPtrHandler(new PtrDefaultHandler() {

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                page = 1;
                refresh.loadMoreComplete(true);
                getFans(true);
            }
        });

        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {

            @Override
            public void loadMore() {
                page++;
                if (page <= totlaPagers){
                    //当前请求页大于总页数，不再请求
                    getFans(false);
                } else {
                    refresh.loadMoreComplete(false);
                }
            }
        });

    }

    private void getFans(final boolean isRefresh){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.CHECK_ID, UCommonUtil.getUtil().getId());
        map.put(BaseConfig.USER_ROLE, BaseConfig.STUDENT_ROLE);
        map.put(BaseConfig.PAGE, page);
        map.put(BaseConfig.PERPAGE, BaseConfig.perpage);
        HttpManager.newInstance(this.getActivity()).query(UserConfig.UserFans.method, UserConfig.UserFans.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                if (isRefresh){
                    refresh.refreshComplete();
                }else {
                    refresh.loadMoreComplete(true);
                }
                UCommonUtil.getUtil().showToast(FragmentClubMemberFans.this.getContext(), "获取失败");
            }

            @Override
            public void onResp(int status, String msg, String data) {
                if (isRefresh){
                    refresh.refreshComplete();
                }else {
                    refresh.loadMoreComplete(true);
                }
                if (status != 0 || data == null){
                    return;
                }
                UserBeanList bean = JsonUtils.getManager().stringToBean(data, UserBeanList.class);
                totlaPagers = bean.getTotalPage();
                List<UserBean> beanList = bean.getData();
                if (beanList != null && beanList.size() > 0){
                    if (isRefresh){
                        adapter.setData(beanList);
                    }else {
                        adapter.getData().addAll(beanList);
                        adapter.notifyDataSetChanged();
                    }
                }

            }
        });
    }


    public void ChooseAll(boolean isCheck){
        List<UserBean> list = adapter.getData();
        if (list != null && list.size() >0){
            for (UserBean bean :list){
                if (!adapter.isMember(String.valueOf(bean.getId()))){
                    bean.setCheck(isCheck);
                }
            }
        }

        adapter.notifyDataSetChanged();

    }

    public List<UserBean> getChoose(){
        List<UserBean> list = adapter.getData();
        List<UserBean> newList = new ArrayList<>();
        if (list != null && list.size() >0){
            for (UserBean bean :list){
                if (bean.isCheck()){
                    newList.add(bean);
                }
            }
        }

        return newList;
    }

    public void notifyData(){
        adapter.notifyDataSetChanged();
    }



}
